/*
 * @Description:
 * @Version: 2.0
 * @Date: 2021-08-24 15:08:55
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-08-30 17:14:44
 */
/*
 * @lc app=leetcode.cn id=145 lang=javascript
 *
 * [145] 二叉树的后序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
// var postorderTraversal = function (root) {
//   if (root == null) {
//     return [];
//   }
//   let postorderTraversalArry = [];
//   postorderTraversalArry = postorderTraversalArry.concat(
//     postorderTraversal(root.left)
//   );
//   postorderTraversalArry = postorderTraversalArry.concat(
//     postorderTraversal(root.right)
//   );
//   postorderTraversalArry.push(root.val);

//   return postorderTraversalArry;
// };
// @lc code=end

var postorderTraversal = function (root) {
  if (root == null) {
    return [];
  }
  let stack = [root];
  let postorderTraversalArry = [];
  while (stack.length) {
    root = stack.pop();
    postorderTraversalArry.push(root.val);
    if (root.left != null) {
      stack.push(root.left);
    }
    if (root.right != null) {
      stack.push(root.right);
    }
  }
  return postorderTraversalArry.reverse();
};
